﻿using Ionic.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Oliving.Common.FileUtilities
{
 


    public static class DotNetZipHelper
    {
        /// <summary>
        /// 压缩文件/文件夹
        /// </summary>
        /// <param name="filePath">需要压缩的文件/文件夹路径</param>
        /// <param name="zipPath">压缩文件路径（zip后缀）</param>
        /// <param name="password">密码</param>
        /// <param name="filterExtenList">需要过滤的文件后缀名</param>
        public static void CompressionFile(string filePath, string zipPath, string password = "", List<string> filterExtenList = null)
        {
            try
            {
                using (ZipFile zip = new ZipFile(Encoding.UTF8))
                {
                    if (!string.IsNullOrWhiteSpace(password))
                    {
                        zip.Password = password;
                    }
                    if (Directory.Exists(filePath))
                    {

                        zip.AddDirectory(filePath);

                    }
                    else if (File.Exists(filePath))
                    {
                        zip.AddFile(filePath, "");
                    }
                    zip.Save(zipPath);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public static void CompressionFile(List<string> filePaths, string zipPath, string password = "")
        {
            try
            {
                using (ZipFile zip = new ZipFile(Encoding.UTF8))
                {
                    if (!string.IsNullOrWhiteSpace(password))
                    {
                        zip.Password = password;
                    }


                    foreach (string fPath in filePaths)
                    {
                        if (Directory.Exists(fPath))
                        {
                            zip.AddDirectory(fPath);
                        }
                        else if (File.Exists(fPath))
                        {
                            zip.AddFile(fPath, "");
                        }
                    }
                    zip.Save(zipPath);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


    }



}
